package ace.tool.generate.code.impl.resolver.ui.resolver;

import ace.tool.generate.code.impl.manager.common.EntityModelManager;
import ace.tool.generate.code.impl.resolver.ui.model.bo.ResponseClassInfo;
import ace.tool.generate.code.impl.resolver.ui.model.bo.UiApiConfigClassInfo;
import ace.tool.generate.code.impl.resolver.ui.model.bo.UiProjectCommonInfo;
import ace.tool.generate.code.impl.resolver.ui.model.bo.UiServerApiClassInfo;
import ace.tool.generate.code.impl.resolver.ui.model.dto.UiClassFileInfo;
import ace.tool.generate.code.impl.service.model.input.GenerateInput;
import ace.tool.generate.code.impl.service.model.input.GenerateModelInput;
import java.util.HashMap;
import java.util.Map;
import lombok.Builder;
import lombok.RequiredArgsConstructor;
import org.apache.commons.text.StringSubstitutor;
import org.apache.commons.text.WordUtils;

/**
 * @author caspar
 * @date 2023/11/17 10:34
 */
@Builder
@RequiredArgsConstructor
public class UiServerApiClassInfoResolver {
  private final GenerateInput input;
  private final GenerateModelInput modelInput;
  private final UiApiConfigClassInfo uiApiConfigClassInfo;
  private final ResponseClassInfo responseClassInfo;
  private final UiProjectCommonInfo uiProjectCommonInfo;
  private final EntityModelManager entityModelManager = EntityModelManager.getInstance();

  public UiServerApiClassInfo resolve() {
    String entityModelId = new EntityModelManager().getEntityModelId(input, modelInput);

    UiClassFileInfo classFileInfo =
        UiClassFileInfoResolver.builder()
            .className(this.modelInput.getName())
            .classNameSuffix(input.getUiServerApiClassNameSuffix())
            .classPackagePrefix(input.getUiServerApiClassPackagePrefix())
            .rootPackageFullName("/core/" + uiProjectCommonInfo.getProjectModuleCodeName())
            .sourceCodeRootDir(uiProjectCommonInfo.getSrcDir())
            .fileNameSuffix(".ts")
            .build()
            .resolve();

    UiServerApiClassInfo result =
        UiServerApiClassInfo.builder()
            .entityModelId(entityModelId)
            .info(classFileInfo)
            .className(classFileInfo.getClassName())
            .uncapitalizeClassName(classFileInfo.getUncapitalizeClassName())
            .moduleApiConfigClassName(this.getModuleApiConfigClassName())
            .moduleApiConfigClassFullName(this.getModuleApiConfigClassFullName())
            .entityResponseClassName(this.getEntityResponseClassName())
            .entityResponseClassFullName(this.getEntityResponseClassFullName())
            .moduleApiConfigFullPath(this.getModuleApiConfigFullPath())
            .build();

    return result;
  }

  private String getModuleApiConfigFullPath() {
    Map<String, String> data = new HashMap<>();
    data.put("moduleApiConfigClassName", this.getModuleApiConfigClassName());
    data.put("modelName", WordUtils.uncapitalize(this.modelInput.getName()));
    return StringSubstitutor.replace("${moduleApiConfigClassName}.${modelName}", data);
  }

  private String getEntityResponseClassFullName() {
    return this.responseClassInfo.getInfo().getClassFullName();
  }

  private String getEntityResponseClassName() {
    return this.responseClassInfo.getInfo().getClassName();
  }

  private String getModuleApiConfigClassFullName() {
    return this.uiApiConfigClassInfo.getInfo().getClassFullName();
  }

  private String getModuleApiConfigClassName() {
    return this.uiApiConfigClassInfo.getInfo().getClassName();
  }
}
